﻿//**************************************************
//CLASS : tcdc_mst_site
//AUTHOR : PATTARAWUT CHANNGAM & SUCHIN INPIRUT
//LAST MODIFIED : Monday, February 25, 2013
//**************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Model
{

public class tcdc_mst_site
{

     private DBUtil DB;
     public string strConn { get; set; }
     public int site_id {get;set;}
     public string site_name_th {get;set;}
     public string site_name_en {get;set;}
     public string site_short_name {get;set;}

     public tcdc_mst_site() {
          this.DB = new DBUtil();
          DB.strConn = strConn;
     }

     public DataTable getById(string id) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_site" + 
                       " WHERE site_id = '" + id + "'";

          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public DataTable getByObject(tcdc_mst_site obj) {

          DataTable dt = new DataTable();
          this.DB = new DBUtil();
          DB.strConn = strConn;

          string sql = "SELECT * FROM tcdc_mst_site " + 
                       "WHERE 1 = 1 ";

          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               sql += "AND site_id LIKE '%" + obj.site_id + "%' ";
          }
          if(obj.site_name_th != null && 
             obj.site_name_th.ToString() != null && 
             obj.site_name_th.ToString() != "0")
          {
               sql += "AND site_name_th LIKE '%" + obj.site_name_th + "%' ";
          }
          if(obj.site_name_en != null && 
             obj.site_name_en.ToString() != null && 
             obj.site_name_en.ToString() != "0")
          {
               sql += "AND site_name_en LIKE '%" + obj.site_name_en + "%' ";
          }
          if(obj.site_short_name != null && 
             obj.site_short_name.ToString() != null && 
             obj.site_short_name.ToString() != "0")
          {
               sql += "AND site_short_name LIKE '%" + obj.site_short_name + "%' ";
          }
          dt = DB.ExecuteReader(sql);

          return dt;

     }

     public int insertData(tcdc_mst_site obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[4];

          prm[0] = new SqlParameter("@site_name_th", SqlDbType.NVarChar,250);
          if(obj.site_name_th != null && 
             obj.site_name_th.ToString() != null && 
             obj.site_name_th.ToString() != "0")
          {
               prm[0].Value = obj.site_name_th;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }

          prm[1] = new SqlParameter("@site_name_en", SqlDbType.NVarChar,250);
          if(obj.site_name_en != null && 
             obj.site_name_en.ToString() != null && 
             obj.site_name_en.ToString() != "0")
          {
               prm[1].Value = obj.site_name_en;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }

          prm[2] = new SqlParameter("@site_short_name", SqlDbType.NVarChar,50);
          if(obj.site_short_name != null && 
             obj.site_short_name.ToString() != null && 
             obj.site_short_name.ToString() != "0")
          {
               prm[2].Value = obj.site_short_name;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }

          prm[3] = new SqlParameter("@ReturnValue", SqlDbType.Int);
          prm[3].Direction = ParameterDirection.Output;

          DB.ExecuteNonQueryProc("sp_tcdc_mst_site_insert", prm, false);
          return (Int32)prm[3].Value;
     }

     public int updateData(tcdc_mst_site obj) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[4];

          prm[0] = new SqlParameter("@site_id", SqlDbType.Int);
          if(obj.site_id != null && 
             obj.site_id.ToString() != null && 
             obj.site_id.ToString() != "0")
          {
               prm[0].Value = obj.site_id;
          }
          else
          {
               prm[0].Value = DBNull.Value;
          }
          prm[1] = new SqlParameter("@site_name_th", SqlDbType.NVarChar,250);
          if(obj.site_name_th != null && 
             obj.site_name_th.ToString() != null && 
             obj.site_name_th.ToString() != "0")
          {
               prm[1].Value = obj.site_name_th;
          }
          else
          {
               prm[1].Value = DBNull.Value;
          }
          prm[2] = new SqlParameter("@site_name_en", SqlDbType.NVarChar,250);
          if(obj.site_name_en != null && 
             obj.site_name_en.ToString() != null && 
             obj.site_name_en.ToString() != "0")
          {
               prm[2].Value = obj.site_name_en;
          }
          else
          {
               prm[2].Value = DBNull.Value;
          }
          prm[3] = new SqlParameter("@site_short_name", SqlDbType.NVarChar,50);
          if(obj.site_short_name != null && 
             obj.site_short_name.ToString() != null && 
             obj.site_short_name.ToString() != "0")
          {
               prm[3].Value = obj.site_short_name;
          }
          else
          {
               prm[3].Value = DBNull.Value;
          }

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_site_update", prm, false);
     }

     public int deleteData(string id) {

          this.DB = new DBUtil();
          DB.strConn = strConn;

          SqlParameter[] prm;
          prm = new SqlParameter[1];

          prm[0] = new SqlParameter("@site_id", id);

          return DB.ExecuteNonQueryProc("sp_tcdc_mst_site_delete", prm, false);
     }
}

}

